package com.wang.springbootinit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.mapper.FeedbackMapper;
import com.wang.springbootinit.mapper.GradeRepository;
import com.wang.springbootinit.model.dto.FeedbackQueryDTO;
import com.wang.springbootinit.model.dto.FeedbackReplyDTO;
import com.wang.springbootinit.model.entity.Feedback;
import com.wang.springbootinit.model.entity.Grade;
import com.wang.springbootinit.model.entity.Student;
import com.wang.springbootinit.model.entity.User;
import com.wang.springbootinit.service.FeedbackService;
import com.wang.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 反馈服务实现类
 * 处理学生反馈的相关业务逻辑
 */
@Service
@Slf4j
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserService userService;

    /**
     * 提交新的反馈
     * @param feedback 反馈信息
     * @return 提交是否成功
     * @throws BusinessException 提交失败时抛出异常
     */
    @Override
    public boolean submitFeedback(Feedback feedback,HttpServletRequest request) {
        Student loginStudent = userService.getLoginStudent(request);
        feedback.setSubmitTime(LocalDateTime.now());
        feedback.setStatus(0); // 设置为待处理状态
        feedback.setStudentId(loginStudent.getId());
        int insert = feedbackMapper.insert(feedback);
        if (insert > 0) {
            return true;
        }
        throw new BusinessException("提交反馈失败");
    }

    /**
     * 回复反馈
     * @param replyDTO 回复信息，包含反馈ID、回复内容和状态
     * @return 回复是否成功
     * @throws BusinessException 当反馈不存在或已关闭时抛出异常
     */
    @Override
    public boolean replyFeedback(FeedbackReplyDTO replyDTO,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Feedback feedback = feedbackMapper.selectById(replyDTO.getFeedbackId());
        if (feedback == null) {
            throw new BusinessException("反馈不存在");
        }
        if (feedback.getStatus() == 2) {
            throw new BusinessException("该反馈已关闭");
        }

        Feedback update = new Feedback();
        update.setFeedbackId(replyDTO.getFeedbackId());
        update.setReplyContent(replyDTO.getReplyContent());
        update.setReplyTime(LocalDateTime.now());
        update.setStatus(1);
        update.setReplyBy(loginUser.getUserName());

        int result = feedbackMapper.updateById(update);
        if (result > 0) {
            return true;
        }
        throw new BusinessException("回复失败");
    }

    /**
     * 分页查询反馈列表
     * @param queryDTO 查询条件，包含学生ID、反馈类型、状态、时间范围等
     * @return 分页后的反馈列表
     */
    @Override
    public Page<Feedback> getFeedbackPage(FeedbackQueryDTO queryDTO) {
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDTO.getStudentId() != null, 
                  Feedback::getStudentId, queryDTO.getStudentId())
               .eq(queryDTO.getFeedbackType() != null, 
                   Feedback::getFeedbackType, queryDTO.getFeedbackType())
               .eq(queryDTO.getStatus() != null, 
                   Feedback::getStatus, queryDTO.getStatus())
               .ge(queryDTO.getStartTime() != null, 
                   Feedback::getSubmitTime, queryDTO.getStartTime())
               .le(queryDTO.getEndTime() != null, 
                   Feedback::getSubmitTime, queryDTO.getEndTime())
               .orderByDesc(Feedback::getSubmitTime);

        Page<Feedback> page = new Page<>(queryDTO.getCurrent(), queryDTO.getPageSize());
        return feedbackMapper.selectPage(page, wrapper);
    }

    /**
     * 获取指定学生的所有反馈
     * @param request
     * @return 该学生的反馈列表，按提交时间降序排序
     */
    @Override
    public List<Feedback> getMyFeedbacks(HttpServletRequest request) {
        Student loginStudent = userService.getLoginStudent(request);
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getStudentId, loginStudent.getId())
               .orderByDesc(Feedback::getSubmitTime);
        return feedbackMapper.selectList(wrapper);
    }

    /**
     * 根据ID获取反馈详情
     * @param feedbackId 反馈ID
     * @return 反馈详情，如果不存在返回null
     */
    @Override
    public Feedback getFeedbackById(Long feedbackId) {
        return null;
    }

    /**
     * 关闭反馈
     * @param replyDTO 反馈ID
     * @return 关闭是否成功
     */
    @Override
    public boolean closeFeedback(FeedbackReplyDTO replyDTO) {
        Feedback feedback = feedbackMapper.selectById(replyDTO.getFeedbackId());
        if (feedback == null) {
            throw new BusinessException("反馈不存在");
        }
        LambdaUpdateWrapper<Feedback> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Feedback::getFeedbackId,feedback.getFeedbackId());
        updateWrapper.set(Feedback::getStatus,2);
        return update(updateWrapper);
    }
} 